package com.xunshibao.util;

import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public final class StringEncryptor {
	private static final Log loger = LogFactory.getLog(StringEncryptor.class);
	private static final char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

	public static String md5(String orig) {
		byte[] strTemp = orig.getBytes();
		MessageDigest mdTemp;
		try {
			mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(strTemp);
			byte[] md = mdTemp.digest();
			int j = md.length;
			char str[] = new char[j*2];
			int k = 0;
			for(int i = 0; i < j; i++) {
				byte bt = md[i];
				str[k++] = hexDigits[bt >>> 4 & 0xf];
				str[k++] = hexDigits[bt & 0xf];
			}
			
			return new String(str);
		} catch (NoSuchAlgorithmException e) {
			loger.error(e.getMessage(), e);
		}
		return null;
	}
	
	 public static String hmacSHA1(String data, String key) {  
         byte[] byteHMAC = null;  
         try {  
             Mac mac = Mac.getInstance("HmacSHA1");  
             SecretKeySpec spec = new SecretKeySpec(key.getBytes(), "HmacSHA1");  
             mac.init(spec);  
             byteHMAC = mac.doFinal(data.getBytes());  
         } catch (InvalidKeyException e) {  
             e.printStackTrace();  
         } catch (NoSuchAlgorithmException ignore) {  
         }  
         String oauth = new BASE64Encoder().encode(byteHMAC);  
         return oauth;
     }  
//	
	public static void main(String[] args) {
//		List<String> list = new ArrayList<String>();
//		list.add("expires=1295510400");
//		list.add("session_key=3.ba08134fb8165600e9a7144c0173db4f.21600.1295510400-178510793");
//		list.add("ss=71cce31f0d3b21d209901538e5ecc679");
//		list.add("user=178510793");
//		
//		Collections.sort(list);
//		
//		StringBuilder sb = new StringBuilder();
//		for(String cv : list) {
//			sb.append(cv);
//		}
//		sb.append("46f7f2a51771447eb7c36629d6fd1fde");
//		
//		String md5 = StringEncryptor.md5(sb.toString());
//		System.out.println(md5);
		System.out.println(DateUtil.getNow());
	}
	
}